//
//  GUIDatePickerView.m
//  DreamOf
//
//  Created by 薛小刚 on 2018/4/4.
//  Copyright © 2018年 薛小刚. All rights reserved.
//

#import "GUIDatePickerView.h"
#import <Masonry.h>
#import "ApplicationSetting.h"

static const short HEIGHT_OF_DATEPICKER = 200;
static const short HEIGHT_OF_DATEPICKER_AND_BUTTON = 230;
@interface GUIDatePickerView()
/// 时间选择器
@property (nonatomic, strong) UIDatePicker *datePicker;
/// 时间选择器的背景的view
@property (nonatomic, strong) UIView *dateBackView;

@property (nonatomic, weak) selectContent myBlock;

@property (nonatomic, assign) DatePickerType type;

@end



@implementation GUIDatePickerView

- (instancetype)initWithDataPickerType:(DatePickerType)type withSelectContent:(selectContent) block {
    if (self = [super init]) {
        _type = type;
        if (type == DatePickerModeDate) {
            self.datePicker.datePickerMode = UIDatePickerModeDate;
        } else if (type == DatePickerModeTime) {
            self.datePicker.datePickerMode = UIDatePickerModeTime;
        } else if (type == DatePickerModeDateAndTime) {
            self.datePicker.datePickerMode = UIDatePickerModeDateAndTime;
        } else if (type == DatePickerModeCountDownTimer) {
            self.datePicker.datePickerMode = UIDatePickerModeCountDownTimer;
        }
        self.myBlock = block;
        self.backgroundColor = [UIColor blackColor];
        self.alpha = 0;
        
    }
    return self;
}

- (UIDatePicker *)datePicker {
    if (_datePicker == nil) {
        _datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, HEIGHT_OF_DATEPICKER_AND_BUTTON - HEIGHT_OF_DATEPICKER, [UIScreen mainScreen].bounds.size.width, HEIGHT_OF_DATEPICKER)];
        
    }
    return _datePicker;
}

- (UIView *)dateBackView {
    if (nil == _dateBackView) {
        _dateBackView = [[UIView alloc] initWithFrame:CGRectMake(0, [UIScreen mainScreen].bounds.size.height, [UIScreen mainScreen].bounds.size.width, HEIGHT_OF_DATEPICKER_AND_BUTTON)];
        _dateBackView.backgroundColor = [UIColor whiteColor];
        [_dateBackView addSubview:self.datePicker];
        
        UIButton * cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [cancelButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        cancelButton.titleLabel.font = [UIFont systemFontOfSize:15];
        cancelButton.frame = CGRectMake(0, 0, 50, HEIGHT_OF_DATEPICKER_AND_BUTTON - HEIGHT_OF_DATEPICKER);
        [_dateBackView addSubview:cancelButton];
        cancelButton.tag = 1;
        [cancelButton addTarget:self action:@selector(cancelButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        
        UIButton * sureButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [sureButton setTitle:@"确定" forState:UIControlStateNormal];
        [sureButton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        sureButton.titleLabel.font = [UIFont systemFontOfSize:15];
        sureButton.frame = CGRectMake([UIScreen mainScreen].bounds.size.width - 50, 0, 50, HEIGHT_OF_DATEPICKER_AND_BUTTON - HEIGHT_OF_DATEPICKER);
        [_dateBackView addSubview:sureButton];
        sureButton.tag = 2;
        [sureButton addTarget:self action:@selector(sureButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _dateBackView;
}

- (void)show {
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow addSubview:self.dateBackView];
    [_dateBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mas_top).offset(0);
        make.bottom.equalTo(self.mas_bottom).offset(0);
        make.left.equalTo(self.mas_left).offset(0);
        make.right.equalTo(self.mas_right).offset(0);
    }];
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0.4;
        self.dateBackView.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2., [UIScreen mainScreen].bounds.size.height - HEIGHT_OF_DATEPICKER_AND_BUTTON / 2.);
    }];
}

- (void) showWithController:(UIViewController *)controller {
    WeakSelf(weSelf, self);
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [[UIApplication sharedApplication].keyWindow addSubview:self.dateBackView];
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(controller.view.mas_top).offset(0);
        make.bottom.equalTo(controller.view.mas_bottom).offset(0);
        make.left.equalTo(controller.view.mas_left).offset(0);
        make.right.equalTo(controller.view.mas_right).offset(0);
    }];
    [self.dateBackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(controller.view.mas_bottom).offset(0);
        make.left.equalTo(controller.view.mas_left).offset(0);
        make.right.equalTo(controller.view.mas_right).offset(0);
        make.height.mas_equalTo(HEIGHT_OF_DATEPICKER_AND_BUTTON);
    }];
    [self.datePicker mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(weSelf.dateBackView.mas_bottom).offset(0);
        make.left.equalTo(weSelf.dateBackView.mas_left).offset(0);
        make.right.equalTo(weSelf.dateBackView.mas_right).offset(0);
        make.height.mas_equalTo(HEIGHT_OF_DATEPICKER);
    }];
    UIButton * cancelBtn = (UIButton *)[_dateBackView viewWithTag:1];
    [cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weSelf.dateBackView.mas_left).offset(0);
        make.top.equalTo(weSelf.dateBackView.mas_top).offset(0);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(HEIGHT_OF_DATEPICKER_AND_BUTTON - HEIGHT_OF_DATEPICKER);
    }];
    UIButton * sureBtn = (UIButton *)[_dateBackView viewWithTag:2];
    [sureBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(weSelf.dateBackView.mas_right).offset(0);
        make.top.equalTo(weSelf.dateBackView.mas_top).offset(0);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(HEIGHT_OF_DATEPICKER_AND_BUTTON - HEIGHT_OF_DATEPICKER);
    }];
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0.4;
        self.dateBackView.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2., [UIScreen mainScreen].bounds.size.height - HEIGHT_OF_DATEPICKER_AND_BUTTON / 2.);
    }];
}


- (void)sureButtonClicked:(UIButton *) button {
    NSDateFormatter * format = [[NSDateFormatter alloc] init];
    if (_type == DatePickerModeDate) {
        [format setDateFormat:@"yyyy-MM-dd"];
    } else if (_type == DatePickerModeTime) {
        [format setDateFormat:@"HH:mm:ss"];
    } else if (_type == DatePickerModeDateAndTime) {
        [format setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    } else {
        
    }
    NSString * dateString = [format stringFromDate:self.datePicker.date];
    self.myBlock(dateString, NO);
    [self hidden];
}

- (void)cancelButtonClicked:(UIButton *) button {
    self.myBlock(nil, YES);
    [self hidden];
}

- (void)hidden {
    [UIView animateWithDuration:0.4 animations:^{
        self.alpha = 0;
        self.dateBackView.center = CGPointMake([UIScreen mainScreen].bounds.size.width / 2., [UIScreen mainScreen].bounds.size.height + HEIGHT_OF_DATEPICKER_AND_BUTTON / 2.);
    } completion:^(BOOL finished) {
        for (UIView * subview in self.subviews) {
            [subview removeFromSuperview];
        }
        for (UIView * subview in self.dateBackView.subviews) {
            [subview removeFromSuperview];
        }
        self.datePicker = nil;
        [self.dateBackView removeFromSuperview];
        self.dateBackView = nil;
        [self removeFromSuperview];
    }];
}

- (void)setMaxDate:(NSDate *)maxDate {
    _maxDate = maxDate;
    self.datePicker.maximumDate = maxDate;
}

- (void)setMinDate:(NSDate *)minDate {
    _minDate = minDate;
    self.datePicker.minimumDate = minDate;
}

- (void) dealloc {
}

@end
